package com.javaweb.newservlelt;

import com.javaweb.common.DBUtil;
import com.javaweb.common.DispatcherTool;
import com.javaweb.common.FileUploadTool;
import com.javaweb.common.StringTool;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.awt.image.DataBuffer;
import java.io.File;
import java.io.IOException;

/**
 * for register and uploading image file.
 */

@WebServlet(name="register",
            urlPatterns = {"/register"}
)
@MultipartConfig(
        maxFileSize = 1024 * 1024 ,
        maxRequestSize = 1024 * 1024 ,
        fileSizeThreshold = 0
)
public class RegisterServlet extends HttpServlet  {
    /**
     * process register, including upload image.
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /**
         * 1. checking parameters
         * 2. checking username
         * 3. checking file type
         * 4. determine the target DIR
         * 5. create a new file name
         * 6. write data into file system
         * 7. add new user into DB
         * 8. return the response.
         */
        req.setCharacterEncoding(StringTool.UTF);
        String name = req.getParameter("name") ;
        String pwd = req.getParameter("pwd" );
        String secondPwd = req.getParameter("secondPwd") ;

        if (StringTool.isEmpty(name, pwd, secondPwd)) {
            DispatcherTool.indirectForwardToClient(req, resp, "02/servlet/register.jsp", "参数为空！");
            return  ;
        }
        if (DBUtil.existedName(name)) {
            DispatcherTool.indirectForwardToClient(req, resp, "02/servlet/register.jsp", "用户名已存在！");
            return  ;
        }
        Part part = req.getPart("userImage") ;
        if (! FileUploadTool.isImage(part.getContentType())) {
            DispatcherTool.indirectForwardToClient(req, resp, "02/servlet/register.jsp", "文件类型不正确！");
            return  ;
        }
        String targetDir = FileUploadTool.getAbsoluteUploadDir(req) ;
        String newFileName = FileUploadTool.newFileName(part.getSubmittedFileName()) ;
        String fullNewFileName = targetDir + File.separator + newFileName ;
        try {
            part.write(fullNewFileName);
        } catch (IOException e) {
            DispatcherTool.indirectForwardToClient(req, resp, "02/servlet/register.jsp", "文件操作出错！");
            return  ;
        }
        int ret = DBUtil.addNewUser(name, pwd, fullNewFileName) ;
        if (ret < 0) {
            DispatcherTool.indirectForwardToClient(req, resp, "02/servlet/register.jsp", "数据库操作出错！");
            return  ;
        }
        DispatcherTool.indirectForwardToClient(req, resp, "02/servlet/register.jsp", "注册成功！");
    }

}
